const mongoUtil = require("./mongo_util")

const {
    createToken
} = require("../middlewares/token")

function getStudentItem(student, callback) {
    const collectionName = "students"
    const mmp = mongoUtil.getCollection(collectionName)
    mmp.then(collection => {
        collection.find({
            "SecondaryCollege": student.SecondaryCollege
            // "classList.className":student.className
        }).toArray(function (err, result) { // 返回集合中所有数据
            if (student.radio === 2) {
                if (result.length > 0) {
                    const Item=[]
                    result[0].classList.forEach((item) => {
                        const adminItem = item.studentList.filter((ite) => {
                            return ite.student_Number === student.student_Number
                        })
                        console.log("adminItem===", adminItem);
                        if(adminItem.length>0){
                            Item.push({...adminItem[0]})
                        }
                        
                    })
                   
                    console.log("adminItem===", Item);
                    // console.log( item.student_Number+'==='+student.student_Number);
                    if (Item.length === 0) {
                        console.log("111111");
                        callback({
                            reason: '您还不是管理员'
                        }, null)
                        return
                    }
                    console.log("222222");
                    if (Item[0].student_Number === student.student_Number && Item[0].password === student.password) {
                        callback(null, {
                            SecondaryCollege: student.SecondaryCollege || undefined,
                            username: student.student_Number,
                            isStudentManage: Item[0].isStudentManage,
                            Authorization: createToken({}, 10)
                        })
                        return
                    }
                    console.log("633333");
                    return
                } else {
                    console.log("hhhhhhhh");
                    callback(err ? err : {
                        reason: "您不是此学院管理员"
                    }, result)
                }
                return
            }
            console.log("44444");
            if (result.length > 0) {
                const classItem = result[0].classList.filter((item) => {
                    return item.className === student.className
                })
                if (classItem.length == 0) {
                    console.log("55555");
                    callback({
                        reason: '此班级不存在'
                    }, null)
                    return
                }
                const studentItem = classItem[0].studentList.filter((ite) => {
                    return ite.student_Number === student.student_Number
                })
                if (studentItem.length > 0) {
                    console.log(studentItem);
                    if (studentItem[0].studentName === student.studentName && studentItem[0].password === student.password) {
                        callback(null, {
                            SecondaryCollege: student.SecondaryCollege || undefined,
                            className: student.className || undefined,
                            // studentItem:studentItem,
                            isStudentManage: studentItem[0].isStudentManage || 0,
                            studentName: student.studentName || undefined,
                            student_Number: student.student_Number || undefined,
                            grade: student.grade || undefined,
                            Authorization: createToken({}, 10)
                        })
                    } else {
                        callback({
                            reason: "账号或密码错误，请重新输入"
                        }, null)
                    }
                } else {
                    console.log("iiiii");
                    callback({
                        reason: '您还没有注册'
                    }, null)
                }
                // new Error()
            } else {
                console.log("HHHhhhhhhh");
                callback(err ? err : {
                    reason: "此学院不存在"
                }, result)
            }
        });
    })
}

module.exports = {
    getStudentItem
}